package com.tcm.doctor.web.controller;

import cn.hutool.json.JSONObject;
import com.base.core.exception.ServiceException;
import com.base.core.utils.R;
import com.tcm.im.dto.*;
import com.tcm.im.service.HxService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/huanxin")
@RestController
public class HxImController {

    @DubboReference(version = "0.0.1-alpha", check = false, timeout =  3000, retries = 3)
    private HxService hxService;

    /**
     * 获取环信token
     */
    @GetMapping("/im/token")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨域
    public R getHxToken() {
        Token token;
        try{
            token = hxService.getToken();
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success(token);
    }

    /**
     * 用户端添加好友
     */
    @GetMapping("/im/user/friend/add")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨域
    public R addUserFriend(@RequestParam String ownUser, @RequestParam String friendUser) {
        try{

            boolean rs = hxService.addUserFriend(ownUser, friendUser);
            if(!rs){
                return R.fail();
            }
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 医生添加好友
     */
    @GetMapping("/im/doctor/friend/add")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨域
    public R addDoctorFriend(@RequestParam String ownUser, @RequestParam String friendUser) {
        try{
            boolean rs = hxService.addDoctorFriend(ownUser, friendUser);
            if(!rs){
                return R.fail();
            }
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 获取环信好友
     */
    @GetMapping("/im/list/friend")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨域
    public R listFriend(@RequestParam Long userId) {
        return R.success(hxService.listFriends(userId));
    }

    /**
     * 获取环信会话列表
     */
    @GetMapping("/im/list/user/chat")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨域
    public R listUserChat(@RequestParam Long userId) {
        return R.success(hxService.listFriendsOnlineForUser(userId));
    }

    /**
     * 获取环信会话列表
     */
    @GetMapping("/im/list/doctor/chat")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨域
    public R listDcotorChat(@RequestParam Long userId) {
        try {
            return R.success(hxService.listFriendsOnlineForDoctor(userId));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 授权添加环信用户
     */
    @PostMapping("/im/add/hxuser")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨域
    public R addHxUser(@RequestBody JSONObject jsonParam) {
        try{
           String username = (String) jsonParam.get("username");
           boolean rs = hxService.addUser(username);
           if(!rs){
               return R.fail(400,"环信注册失败");
           }
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }



    /**
     * 保存聊天会话信息
     */
    @PostMapping("/im/chat/message/save")
    @CrossOrigin(origins = "*", maxAge = 3600)//跨域
    public R sendMessage(@RequestBody ImChatDTO msg) {
        String chatId = null;
        try{
            chatId = hxService.saveSessionData(msg);
        }catch (ServiceException e){
            return R.fail(e.getResult().getCode(), e.getMessage());
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        Map<String, String> rs = new HashMap<>();
        rs.put("chatId", chatId);
        return R.success(rs);
    }

    @ResponseBody
    @RequestMapping(value = "/invite/friend", method = RequestMethod.GET)
    public R saveDoctorPatient(String params){
        try {
            hxService.saveDoctorPatient(params);
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
        return R.success();
    }


}
